package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MemberService.class )
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    //根据手机号码查询会员信息
    @Override
    public Member findByTelePhone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //新增保存会员信息（多个服务可能调用）
    @Override
    public void add(Member member) {

        String password = member.getPassword();
        if(password != null){
            //将明文密码利用MD5进行加密
            member.setPassword(MD5Utils.md5(password));
        }
        memberDao.add(member);
    }

    //根据月份集合查询数据库，获得会员数量集合
    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {

        ArrayList<Integer> list = new ArrayList<>();
        for (String month : months) {//2019.03

            Integer memberCountBeforeDate = memberDao.findMemberCountBeforeDate(month + ".31");//2019.03.31

            list.add(memberCountBeforeDate);
        }

        return list;
    }


}
